/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package koor.action.coursework;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import koor.action.Action;
import koor.action.ActionResult;
import koor.entity.CourseWork;
import koor.persistent.CourseWorkDAO;
import koor.persistent.StudentDAO;
import koor.persistent.SubjectDAO;
import koor.persistent.TeacherDAO;
import koor.service.ServiceLocator;
import java.sql.Date;

/**
 * The
 * <code>CourseWorkSaveAction</code> class represents action, used to save courseWork
 * information
 *
 * @author Admin
 */
public class CourseWorkSaveAction implements Action {

    @Override
    public String getName() {
        return "courseWork.save";
    }

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {

        if ((request.getParameter("id") == null) || (request.getParameter("id").isEmpty())){
            request.getSession().setAttribute("fail", "Неверные данные");
            return ActionResult.redirect("courseWork.list");
        }

        // if Save button have been pressed
        if (request.getParameter("saveButton") != null) {
            try {
                Long id = Long.parseLong(request.getParameter("id"));
                CourseWork courseWork = (id == -1) ? new CourseWork() : ServiceLocator.getService(CourseWorkDAO.class).findOne(id);
                StudentDAO studentDAO = ServiceLocator.getService(StudentDAO.class);
                TeacherDAO teacherDAO = ServiceLocator.getService(TeacherDAO.class);
                SubjectDAO subjectDAO = ServiceLocator.getService(SubjectDAO.class);
                CourseWorkDAO courseWorkDAO = ServiceLocator.getService(CourseWorkDAO.class);
                // load data from request
                courseWork.setTheme(request.getParameter("theme"));
                courseWork.setAuthor(studentDAO.findOne(Long.parseLong(request.getParameter("student_id"))));
                courseWork.setLeader(teacherDAO.findOne(Long.parseLong(request.getParameter("teacher_id"))));
                courseWork.setSubject(subjectDAO.findOne(Long.parseLong(request.getParameter("subject_id"))));
                courseWork.setDirection(request.getParameter("direction"));
                
                if ((request.getParameter("consultant") != null)){
                    courseWork.setConsultant(request.getParameter("consultant"));
                }
                if ((request.getParameter("mark") != null) || (!request.getParameter("mark").isEmpty())){
                    courseWork.setMark(Integer.parseInt(request.getParameter("mark")));
                }
                if ((request.getParameter("date") != null) || (!request.getParameter("date").isEmpty())){
                    courseWork.setDate(Date.valueOf(request.getParameter("date")));
                }
                if ((request.getParameter("placement") != null)){
                    courseWork.setPlacement(request.getParameter("placement"));
                } 
                if ((request.getParameter("inventory_number") != null)){
                    courseWork.setInventoryNumber(request.getParameter("inventory_number"));
                } 

                if (id != -1) {
                    courseWorkDAO.update(courseWork); // update
                    request.getSession().setAttribute("win", courseWork.getTheme() + " была обновлена");
                } else {
                    courseWorkDAO.save(courseWork); // create
                    request.getSession().setAttribute("win", courseWork.getTheme() + " была добавлена");
                }
            } catch (Exception e) {
                request.getSession().setAttribute("fail", "Не удалось сохранить");
            }
        } else {
            request.getSession().setAttribute("fail", "Не удалось сохранить");
        }

        return ActionResult.redirect("courseWork.list");
    }
}
